package com.bianguo.android.apushdemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.utils.MyLog;

/**
 * Created by Administrator on 2017/8/31.
 */

public class TitleView extends View {

    private Paint mPaint;
    private Rect mBound;
    private int mTitleTextColor;
    private String mTitleText;

    public TitleView(Context context) {
        super(context);
        initView(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context mContext, AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TitleView);
        mTitleText = typedArray.getString(R.styleable.TitleView_title);
        float titleSize = typedArray.getDimension(R.styleable.TitleView_titleSize, 14);
        mTitleTextColor = typedArray.getColor(R.styleable.TitleView_titleColor, Color.RED);

        MyLog.mLog("title = " + mTitleText + ", titleSize = " + titleSize + ", color = " + mTitleTextColor);
        typedArray.recycle();

        mPaint = new Paint();
        mBound = new Rect();
        mPaint.setTextSize(titleSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
    }
}
